package view.macrogui
{
	import events.WorldEvent;
	import flash.display.Sprite;
	import flash.text.TextField;
	import model.macro.Building;
	
	/**
	 */
	public class BuildingList extends Sprite implements SidebarItem
	{
		private var items:Vector.<BuildingListItem>;
		static private const PADDING:Number = 3;
		
		static private const ROWS:int = int((SideBar.WIDTH - 2 * SideBar.PADDING) / (BuildingListItem.WIDTH + PADDING));
		
		public function BuildingList()
		{
			items = new Vector.<BuildingListItem>();
		}
		
		public function update(buildings:Vector.<Building>):void
		{
			if (buildings == null)
			{
				buildings = new Vector.<Building>();
			}
			
			while (buildings.length >= items.length)
			{
				var item:BuildingListItem = new BuildingListItem();
				addChild(item);
				items.push(item);
			}
			for (var i:int = 0; i < buildings.length; i++)
			{
				items[i].visible = true;
				items[i].update(buildings[i]);
				items[i].x = (i % ROWS) * (BuildingListItem.WIDTH + PADDING);
				items[i].y = int(i / ROWS) * (BuildingListItem.HEIGHT + PADDING);
			}
			for (; i < items.length; i++)
			{
				items[i].visible = false;
			}
		}
		
		public function getHeight():int
		{
			if (visible)
			{
				return 150;
			}
			else
				return 0;
		}
		
		private function getWidth():int
		{
			return SideBar.WIDTH - SideBar.PADDING * 2;
		}
	
	}

}